package org.itboys.weixin.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.weixin.entity.WeixinSendInfo;
import org.itboys.weixin.service.WeixinSendInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 推送用的信息模板
 * @author weisky
 *
 * 2016年4月30日
 */
@RestController 
@RequestMapping("/weixin/sendinfotemplate") 
public class WeixinSendInfoController extends BaseController{

	@Autowired
	private WeixinSendInfoService weixinSendInfoService;
	
	/**
	 * 跳到管理页面 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/weixin/weixins/sendinfotemplate");
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Page<WeixinSendInfo> page = weixinSendInfoService.listByPage(request);
		JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
	} 
	
	/**
	 * 保存会员信息
	 * @param member
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(WeixinSendInfo weixinSendInfo, 
			HttpServletRequest request, HttpServletResponse response){
		boolean result = weixinSendInfoService.insert(weixinSendInfo);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 根据会员的openId获取会员信息
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getById")
	public void getById(Long id, HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, weixinSendInfoService.getById(id));
	}
	
	/**
	 * 删除
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delete")
	public void delete(Long id, HttpServletRequest request, HttpServletResponse response){
		try{
			weixinSendInfoService.delete(id);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("WeixinSendInfoController.delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
		
	}
}
